<?php
class DocumentsController extends Zend_Controller_Action
{	
    public function init() {
        
    }
    public function indexAction()
    {
        $this->view->title = "Tài liệu - văn bản";
        $where = "1 = 1";
        $getfoder = $this->_request->getParam('forder', false);
        $gettype = $this->_request->getParam('type', false);
        $getwhere = $this->_request->getParam('where', false);
        $getkey = $this->_request->getParam('key', false);
        $getdatefrom = $this->_request->getParam('date_from', false);
        $getdateto = $this->_request->getParam('date_to', false);
        $getkeyword = $this->_request->getParam('keyword', false);
        $getyear = $this->_request->getParam('year', false);
        
        if($getfoder != NULL){
            $where .= " and Do.type_foder_id = $getfoder";
            $this->view->getforder = $getfoder;
        }
        if($gettype != NULL){
            $where .= " and Do.type_doc_id = $gettype";
            $this->view->gettype = $gettype;
        }
        if($getwhere != NULL){
            $where .= " and Do.where_id = $getwhere";
            $this->view->getwhere = $getwhere;
        }
        if($getkey != NULL){
            $where .= " and Do.key_number like '%".$getkey."%'";
            $this->view->getkey = $getkey;
        }
        if($getdatefrom != NULL){
            $where .= " and Do.date >= '".strtotime($getdatefrom)."'";
            $this->view->getdatefrom = $getdatefrom;
        }
        if($getdateto != NULL){
            $where .= " and Do.date <= '".strtotime($getdateto)."'";
            $this->view->getdateto = $getdateto;
        }
        if($getkeyword != NULL){
            $where .= " and Do.title like '%".$getkeyword."%'";
            $this->view->getkeyword = $getkeyword;
        }
        if($getyear != NULL){
            $where .= " and FROM_UNIXTIME(date, '%Y') = $getyear";
            
        }
    
        $modelDev = new Vina_Model_DeveloperMapper();
        $dataList = $modelDev->fetchListCondition($where);
    
        $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            
        $modelTypeDoc = new Vina_Model_TypeDoc();
        $dataTypeDoc = $modelTypeDoc->fetchListToArray('1=1', 'id desc');
        
        $modelTypeFoder = new Vina_Model_TypeFoder();
        $dataTypeForder = $modelTypeFoder->fetchListToArray('1=1', 'id desc');
        
        $modelTypeWhere = new Vina_Model_WhereStart();
        $dataTypeWhere = $modelTypeWhere->fetchListToArray('1=1', 'id desc');
        
        $this->view->dataType = $dataTypeDoc;
        $this->view->dataForder = $dataTypeForder;
        $this->view->dataWhere = $dataTypeWhere;
     
     
            $this->view->data = $paginator;
    }
    public function searchAction(){
        $formData = $this->_request->getPost();
        $string = "";
        foreach($formData as $key=>$vl):
            if($vl != NULL){
                $string .= "/".$key."/".$vl;
            }
        endforeach;
        $this->_redirect("/documents/index".$string);
    }	
}
